package com.gamemost.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.os.Environment;

import com.gamemost.templestay.Config;
import com.gamemost.templestay.R;

public class ResourceDatasource {
	  private static ArrayList<ResourceData> getImageResourceArray()
	  {
		    ArrayList resourceArray = new ArrayList();

		    ResourceData resourceData = new ResourceData();
		    resourceData.resourceId = R.drawable.afd_bookmark;
		    resourceData.resourceName = "afd_bookmark.png";
		    resourceArray.add(resourceData);
		    resourceData = null;
		    
		    resourceData = new ResourceData();
		    resourceData.resourceId = R.drawable.afd_bookmark_yellow;
		    resourceData.resourceName = "afd_bookmark_yellow.png";
		    resourceArray.add(resourceData);
		    return resourceArray;

	  }
	private static ArrayList<ResourceData> getJSResourceArray() {
		ArrayList resourceArray = new ArrayList();
		ResourceData resourceData = new ResourceData();

		resourceData.resourceId = R.raw.control;
		resourceData.resourceName = "control.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.jtoc;
		resourceData.resourceName = "jtoc.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.jquery;
		resourceData.resourceName = "jquery.min.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.jsetting;
		resourceData.resourceName = "jsetting.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.jindex;
		resourceData.resourceName = "jindex.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.rangyserializer;
		resourceData.resourceName = "rangyserializer.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.rangycore;
		resourceData.resourceName = "rangycore.js";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.androidselection;
		resourceData.resourceName = "androidselection.js";
		resourceArray.add(resourceData);
		resourceData = null;

		return resourceArray;
	}

	private static ArrayList<ResourceData> getCSSResourceArray() {
		ArrayList resourceArray = new ArrayList();
		ResourceData resourceData = new ResourceData();

		resourceData.resourceId = R.raw.sindex;
		resourceData.resourceName = "sindex.css";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.readpage;
		resourceData.resourceName = "readpage.css";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.ssetting;
		resourceData.resourceName = "ssetting.css";
		resourceArray.add(resourceData);
		resourceData = null;

		resourceData = new ResourceData();
		resourceData.resourceId = R.raw.stoc;
		resourceData.resourceName = "stoc.css";
		resourceArray.add(resourceData);
		resourceData = null;

		return resourceArray;
	}

	private static void writeFilesToSdacrd(
			ArrayList<ResourceData> resourceArray, String path, Context context) {
		String dir = Environment.getExternalStorageDirectory().getPath() + path;
		for (int i = 0; i < resourceArray.size(); i++) {
			int fileID = ((ResourceData) resourceArray.get(i)).resourceId;
			String fileName = ((ResourceData) resourceArray.get(i)).resourceName;
			String pathName = dir + "/" + fileName;
			File file = new File(pathName);
			OutputStream output = null;
			System.out.println("writeFilesToSdacrd start "+pathName);

			if (!file.exists() || Config.copyRes) {
				try {
					InputStream input = context.getResources().openRawResource(
							fileID);

					File f = new File(dir);
					if (!f.exists())
						f.mkdir();
					System.out.println("writeFilesToSdacrd "+fileName);
					file.createNewFile();
					output = new FileOutputStream(file);
					byte[] buffer = new byte[4096];
					int len;
					while ((len = input.read(buffer)) != -1) {
						// int len;
						output.write(buffer, 0, len);
					}
					output.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void copyResourceFiles(Context context) {

		ArrayList jsArray = new ArrayList();
		jsArray = getJSResourceArray();
		writeFilesToSdacrd(jsArray, Config.JS, context);

		ArrayList cssArray = new ArrayList();
		cssArray = getCSSResourceArray();
		writeFilesToSdacrd(cssArray, Config.CSS, context);
		
		ArrayList imageArray = new ArrayList();
		imageArray = getImageResourceArray();
		writeFilesToSdacrd(imageArray, Config.IMG, context);

	}
}
